using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using HeadspringExample.Core.Domain;
using HeadspringExample.Core.Domain.Model;
using HeadspringExample.Infrastructure.DataAccess;
using HeadspringExample.Infrastructure.DataAccess.Repositories;
using HeadspringExample.UI.Models;
using HeadspringExample.UI.Helpers;

namespace HeadspringExample.UI.Controllers
{
	public class ConferenceController : DefaultController
	{
		private readonly IConferenceRepository _repository;

		public ConferenceController(
			IConferenceRepository conferenceRepository)
		{
			_repository = conferenceRepository;
		}

		public ActionResult Index(int? minSessions)
		{
			minSessions = minSessions ?? 0;
			
			Conference[] list = _repository.Query()
				.Where(c => c.SessionCount >= minSessions)
				.ToArray();
			
			return AutoMapView<ConferenceListModel[]>(View(list));
		}

		public ActionResult Show(Conference eventName)
		{
			return AutoMapView<ConferenceShowModel>(View(eventName));
		}

		public ActionResult Edit(Conference eventName)
		{
			return AutoMapView<ConferenceEditModel>(View(eventName));
		}

		[HttpPost]
		public ActionResult Edit(ConferenceEditModel form)
		{
			var successResult = this.RedirectToAction(c => c.Index(null), "Default");

			return Form(form, successResult);
		}
	}
}
